<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');
die;

  $now = SimpleDate::fromStamp(time() + LAG);
  $now->hours = 0;
  $now->minutes = 0;
  $now->seconds = 0;

$sql="select * from ns_v1myorder";
$orderarr = getall($sql,1);

//print_r($orderarr);

   if(!empty($orderarr))
   {
   	 foreach ($orderarr as $oneo)
   	 {
   	 	if($oneo['type'] == 'Wedding')
   	 	{
   	 		$preconarr = array(
						      'trackId' => MiscUtils::unique(),
						      'parent_trackId' => '',
						      'cancelled' => ($oneo['disposalstatus']== 1) ? 1 : 0,
						      'completed' => 0,
						      'modified' => 0,
						      'branch' => BRANCH,
						      'host' => 'http://' . DOMAIN . PATH,
						      'state' => ($oneo['date']) ? 1 : 0,
						      'deadline' => ($oneo['disposalstatus'] == 1) ? 0 : (($oneo['date']) ? SimpleDate::toStamp(SimpleDate::fromStamp($oneo['date'] - 21 * 86400)) : 0),
						      'title' => 'Precon',
						      'estimates' => 0,
						      'extras' => 0,
						      'travels' => 0,
						      'createdDate' => SimpleDate::toStamp($now),
						      'sentDate' => 0,
						      'receivedDate' => 0,
						      'receiver' => '',
						      'address' => '',
						      'code' => '',
						      'company' => '',
						      'weight' => 0,
						      'comment' => '',
						      'review' => '',
						      'pi_trackId' => MiscUtils::unique(),
						      'pi_sampleId' => 0,
						      'pi_name' => '',
						      'pi_price' => 0,
						      'pi_unit' => '',
						      'pi_type' => '',
						      'pi_model' => MODEL_NAME_PRECON,
						      'pi_otherId' => '',
						      'pi_quantity' => 0,
						      'pi_total' => 0,
						      'pi_packageInc' => '',
						      'user_trackId' => '',
						      'user_account' => '',
						      'user_password' => '',
						      'user_name' => '',
						      'user_email' => '',
						      'user_type' => '',
						      'when' => 0,
						      'how' => '',
						      'date' => ($oneo['date']) ? $oneo['date'] : 0,
						      'orderFlag' => $oneo['flag'],
						      'orderTrackId' => $oneo['trackId'],
						      'orderId' => $oneo['e_oid'],
						      'customerId' => $oneo['customerId'],
						      'note' => '',
						      'owner' => '',
						      'ownerTrackId' => '',
						      'isCustom' => 0,
						      'customTask' => '',
						      'urgent' => 0  	 		
   	 		
   	 		                  );
   	 		                  
   	 		                 // print_r($preconarr);
   	 		                 insert1('ns_produce',$preconarr);
   	 	}//precon
   	 	
   	 	
   	 	if(!empty($oneo['videoname']))
   	 	{
   	 		$vmusicarray =array(
						      'trackId' => MiscUtils::unique(),
						      'parent_trackId' => '',
						      'cancelled' => ($oneo['disposalstatus']== 1) ? 1 : 0,
						      'completed' => 0,
						      'modified' => 0,
						      'branch' => BRANCH,
						      'host' => 'http://' . DOMAIN . PATH,
						      'state' => ($oneo['date']) ? 1 : 0,
						      'deadline' => ($oneo['disposalstatus'] == 1) ? 0 : (($oneo['date']) ? SimpleDate::toStamp(SimpleDate::fromStamp($oneo['date'] - 21 * 86400)) : 0),
						      'title' => 'Video Music',
						      'estimates' => 0,
						      'extras' => 0,
						      'travels' => 0,
						      'createdDate' => SimpleDate::toStamp($now),
						      'sentDate' => 0,
						      'receivedDate' => 0,
						      'receiver' => '',
						      'address' => '',
						      'code' => '',
						      'company' => '',
						      'weight' => 0,
						      'comment' => '',
						      'review' => '',
						      'pi_trackId' => MiscUtils::unique(),
						      'pi_sampleId' => 0,
						      'pi_name' => '',
						      'pi_price' => 0,
						      'pi_unit' => '',
						      'pi_type' => '',
						      'pi_model' => MODEL_NAME_MUSIC,
						      'pi_otherId' => '',
						      'pi_quantity' => 0,
						      'pi_total' => 0,
						      'pi_packageInc' => '',
						      'user_trackId' => '',
						      'user_account' => '',
						      'user_password' => '',
						      'user_name' => '',
						      'user_email' => '',
						      'user_type' => '',
						      'when' => 0,
						      'how' => '',
						      'date' => ($oneo['date']) ? $oneo['date'] : 0 ,
						      'orderFlag' => $oneo['flag'],
						      'orderTrackId' =>$oneo['trackId'],
						      'orderId' => $oneo['e_oid'],
						      'customerId' => $oneo['customerId'] ,
						      'note' => '',
						      'owner' => '',
						      'ownerTrackId' => '',
						      'isCustom' => 0,
						      'customTask' => '',
						      'urgent' => 0 	 		
   	 		
   	 		                   );
   	 		                   
   	 		                  // print_r($vmusicarray);
   	 		                  insert1('ns_produce',$vmusicarray);
   	 		                   
   	 		                   
   	 		     $reqarray = array(
							      'trackId' => MiscUtils::unique(),
							      'parent_trackId' => '',
							      'cancelled' => ($oneo['disposalstatus'] == 1) ? 1 : 0,
							      'completed' => 0,
							      'modified' => 0,
							      'branch' => BRANCH,
							      'host' => 'http://' . DOMAIN . PATH,
							      'state' => ($oneo['date']) ? 1 : 0,
							      'deadline' => ($oneo['disposalstatus'] == 1) ? 0 : (($oneo['date']) ? SimpleDate::toStamp(SimpleDate::fromStamp($oneo['date'] - 21 * 86400)) : 0),
							      'title' => 'Video Requirement Form',
							      'estimates' => 0,
							      'extras' => 0,
							      'travels' => 0,
							      'createdDate' =>  SimpleDate::toStamp($now),
							      'sentDate' => 0,
							      'receivedDate' => 0,
							      'receiver' => '',
							      'address' => '',
							      'code' => '',
							      'company' => '',
							      'weight' => 0,
							      'comment' => '',
							      'review' => '',
							      'pi_trackId' => MiscUtils::unique(),
							      'pi_sampleId' => 0,
							      'pi_name' => '',
							      'pi_price' => 0,
							      'pi_unit' => '',
							      'pi_type' => '',
							      'pi_model' => MODEL_NAME_REQUIREMENT,
							      'pi_otherId' => '',
							      'pi_quantity' => 0,
							      'pi_total' => 0,
							      'pi_packageInc' => '',
							      'user_trackId' => '',
							      'user_account' => '',
							      'user_password' => '',
							      'user_name' => '',
							      'user_email' => '',
							      'user_type' => '',
							      'when' => 0,
							      'how' => '',
							      'date' => ($oneo['date']) ? $oneo['date'] : 0,
							      'orderFlag' => $oneo['flag'],
							      'orderTrackId' => $oneo['trackId'],
							      'orderId' => $oneo['e_oid'],
							      'customerId' => $oneo['customerId'],
							      'note' => '',
							      'owner' => '',
							      'ownerTrackId' => '',
							      'isCustom' => 0,
							      'customTask' => '',
							      'urgent' => 0   	 		     
   	 		     
   	 		                       );  

   	 		                      // print_r($reqarray);
   	 		                      insert1('ns_produce',$reqarray);
   	 		
   	 	}
   	 	
   	 	
   	 	   $photoarray = getPurchaseItemByorderid($oneo['e_oid'],'Photographer');
   	 	   
   	 	   if(!empty($photoarray))
   	 	   {
   	 	   	 foreach ($photoarray as $onepgher)
   	 	   	 {
   	 	   	 	
   	 	   	 	
   	 	   	 	$pgherarr = array(
								  'trackId' =>MiscUtils::unique(),
								  'parent_trackId' =>'',
								  'cancelled' =>($oneo['disposalstatus'] == 1) ? 1 : 0,
								  'completed' =>0,
								  'modified' =>0,
								  'branch' =>BRANCH,
								  'host' =>'http://' . DOMAIN . PATH,
								  'state' =>($oneo['date']) ? 1 : 0,
								  'deadline' =>($oneo['disposalstatus'] == 1) ? 0 : (($oneo['date']) ? SimpleDate::toStamp(SimpleDate::fromStamp($oneo['date'] - 21 * 86400)) : 0),
								  'title' =>$onepgher['model'],
								  'estimates' =>$onepgher['quantity'],
								  'extras' =>0,
								  'travels' =>0,
								  'createdDate' =>SimpleDate::toStamp($now),
								  'sentDate' =>0,
								  'receivedDate' =>0,
								  'receiver' =>'',
								  'address' =>'',
								  'code' =>'',
								  'company' =>'',
								  'weight' =>0,
								  'comment' =>'',
								  'review' =>'',
								  'pi_trackId' =>$onepgher['trackId'],
								  'pi_sampleId' =>$onepgher['sampleId'],
								  'pi_name' =>$onepgher['name'],
								  'pi_price' =>$onepgher['price'],
								  'pi_unit' =>$onepgher['unit'],
								  'pi_type' =>$onepgher['type'],
								  'pi_model' =>$onepgher['model'],
								  'pi_otherId' =>$onepgher['otherId'],
								  'pi_quantity' =>$onepgher['quantity'],
								  'pi_total' =>$onepgher['total'],
								  'pi_packageInc' =>empty($onepgher['packageInc']) ? '' : $onepgher['packageInc'],
								  
								  'user_trackId' =>'',
								  'user_account' =>'',
								  'user_password' =>'',
								  'user_name' =>'',
								  'user_email' =>'',
								  'user_type' =>'',
								  'when' =>0,
								  'how' =>'',
								  'date' =>($oneo['date']) ? $oneo['date'] : 0,
								  'orderFlag' =>$oneo['flag'],
								  'orderTrackId' =>$oneo['trackId'],
								  'orderId' =>$oneo['e_oid'],
								  'customerId' =>$oneo['customerId'],
								  'note' =>'',
								  'owner' =>'',
								  'ownerTrackId' =>'',
								  'isCustom' =>0,
								  'customTask' =>'',
								  'urgent' =>0
   	 	   	 	                  );
   	 	   	 	                  
   	 	   	 	                   //print_r($pgherarr);
   	 	   	 	                   insert1('ns_produce',$pgherarr);
   	 	   	 }
   	 	   	
   	 	   }//摄影师流程结束
   	 	   
   	 	   
           $videoarray = getPurchaseItemByorderid($oneo['e_oid'],'Videographer');
           
           if(!empty($videoarray))
           {
             foreach ($videoarray as $onevgher)
             {
                
                
                $vgherarr = array(
                                  'trackId' =>MiscUtils::unique(),
                                  'parent_trackId' =>'',
                                  'cancelled' =>($oneo['disposalstatus'] == 1) ? 1 : 0,
                                  'completed' =>0,
                                  'modified' =>0,
                                  'branch' =>BRANCH,
                                  'host' =>'http://' . DOMAIN . PATH,
                                  'state' =>($oneo['date']) ? 1 : 0,
                                  'deadline' =>($oneo['disposalstatus'] == 1) ? 0 : (($oneo['date']) ? SimpleDate::toStamp(SimpleDate::fromStamp($oneo['date'] - 21 * 86400)) : 0),
                                  'title' =>$onevgher['model'],
                                  'estimates' =>$onevgher['quantity'],
                                  'extras' =>0,
                                  'travels' =>0,
                                  'createdDate' =>SimpleDate::toStamp($now),
                                  'sentDate' =>0,
                                  'receivedDate' =>0,
                                  'receiver' =>'',
                                  'address' =>'',
                                  'code' =>'',
                                  'company' =>'',
                                  'weight' =>0,
                                  'comment' =>'',
                                  'review' =>'',
                                  'pi_trackId' =>$onevgher['trackId'],
                                  'pi_sampleId' =>$onevgher['sampleId'],
                                  'pi_name' =>$onevgher['name'],
                                  'pi_price' =>$onevgher['price'],
                                  'pi_unit' =>$onevgher['unit'],
                                  'pi_type' =>$onevgher['type'],
                                  'pi_model' =>$onevgher['model'],
                                  'pi_otherId' =>$onevgher['otherId'],
                                  'pi_quantity' =>$onevgher['quantity'],
                                  'pi_total' =>$onevgher['total'],
                                  'pi_packageInc' =>empty($onevgher['packageInc']) ? '' : $onevgher['packageInc'],
                                  
                                  'user_trackId' =>'',
                                  'user_account' =>'',
                                  'user_password' =>'',
                                  'user_name' =>'',
                                  'user_email' =>'',
                                  'user_type' =>'',
                                  'when' =>0,
                                  'how' =>'',
                                  'date' =>($oneo['date']) ? $oneo['date'] : 0,
                                  'orderFlag' =>$oneo['flag'],
                                  'orderTrackId' =>$oneo['trackId'],
                                  'orderId' =>$oneo['e_oid'],
                                  'customerId' =>$oneo['customerId'],
                                  'note' =>'',
                                  'owner' =>'',
                                  'ownerTrackId' =>'',
                                  'isCustom' =>0,
                                  'customTask' =>'',
                                  'urgent' =>0
                                  );
                                  
                                  //print_r($vgherarr);
                                  insert1('ns_produce',$vgherarr);
             }
            
           }//摄像师流程结束   	 	   
   	 	
   	 }
   	
   }

 

 
 
 
 
 
 
 function insert1($table,$arra,$debug='')
 {
    global  $myPdo1;
    
              if(!empty($arra))
              {
                $expensevalue = '';
                $fieldsexpense = '';
                
                foreach($arra as $key => $value)
                {
                         $fieldsexpense.= '`'.$key.'`,';
                         $expensevalue .=" '".addslashes($value)."',";                   
                }
                
                //$fieldsexpense .= "cancelledMessage";
                $fieldsexpense =substr($fieldsexpense,0,-1);
                //$expensevalue .= "'0'";
                $expensevalue =substr($expensevalue,0,-1);
                
                $expensesql = " insert into {$table}(".$fieldsexpense.") values(".$expensevalue.")";
                
                if(!empty($debug))
                {
                    echo "EXECUTE SQL:\n".$expensesql."\n";
                    //return 0;     
                }else{
                    sql_execute1($expensesql);
                   // return  lastinsertid();
                }   
              }
    
 }
 
 function getSourceName ($source_id)
 {
    global $myPdo;
    
    $sql="SELECT source_name FROM customer_source where source_id = '{$source_id}' limit 1";
    
    $sourarr = getall($sql,1);
    
    return $sourarr[0]['source_name'];
 }

 /**
  * 
  */
 function getPackageInfo($photopackage,$packageprice)
 {
    global $myPdo;
    
    $photopackagesql = "select abs(a.price - {$packageprice}) AS cha,a.* FROM ns_v1package AS a where a.`name` = '{$photopackage}'  ORDER BY cha ASC  limit 1";
                  
    $photopackageinfo = getall($photopackagesql,1);
    
    if(!empty($photopackageinfo))
    {
        $sqla = "SELECT ifnull(SUM(quantity*total),0) AS total FROM ns_v1packageitem WHERE package_id = '{$photopackageinfo[0]['e_oid']}'";
        $originalarr = getall($sqla,1);
        $originalprice = $originalarr[0]['total'] +$photopackageinfo[0]['hours']*180 ;
        $photopackageinfo[0]['original'] = $originalprice;
        return $photopackageinfo[0];
    }else{
         return  $photopackageinfo;
    } 
      
             
 }
 
 /**
  * 
  * get purchaseitem
  */
  //SELECT a.sample_id,b.`name`,b.price,b.unit,b.type,b.model,a.otherId,a.quantity,(a.quantity*b.price) AS total,'' AS packageInc,0 AS isFree,0 AS urgent,NULL AS order_id,a.e_oid FROM ns_v1packageitem AS a LEFT JOIN  ns_v1sample AS b ON a.sample_id =b.e_oid; 
  
 function getPurchaseItem($package_id)
 {
    global $myPdo;
    
    $sql="SELECT a.sample_id,b.`name`,b.price,b.unit,b.type,b.model,a.otherId,a.quantity,(a.quantity*b.price) AS total,'' AS packageInc,0 AS isFree,0 AS urgent,NULL AS order_id,a.e_oid
     FROM ns_v1packageitem AS a LEFT JOIN  ns_v1sample AS b ON a.sample_id =b.e_oid
     where a.package_id = '{$package_id}'
  ";
    
    $purchaseiteminfo = getall($sql,1);
    
    return $purchaseiteminfo;
 }
 
 /**
  * get purchaseitemselect 
  * 
  */
  function getPurchaseItemSelect ($packageitem_id)
  {
    global $myPdo;
    
    $sql="SELECT * FROM ns_v1packageitemselect WHERE packageitem_id = '{$packageitem_id}'";
    $psarr = getall($sql,1);
    
    return $psarr;
    
    
  }
 
  /**
   * myPdo 1
   * 
   *
 * 执行sql
 * */
function sql_execute1($sql){
    global $myPdo1;  
    
    $stmt2 = $myPdo1->prepare($sql);
    $stmt2->execute();

    return;
}

/**
 * get purchaseitmem
 * 
 */
  function getPurchaseItemByorderid($order_id,$model)
  {
  	global $myPdo;
  	
  	$sql="SELECT * FROM ns_v1purchaseitem WHERE order_id= '{$order_id}' and model = '{$model}'";
  	
  	$purphotoarr = getall($sql,1);
  	
  	return $purphotoarr;
  	
  } 















?>